//
//  XLog.swift
//  TuneMate
//
//  Created by 丸子 on 2023/11/23.
//

import Foundation

class XLog {
    static func d(_ tag: String, _ items: Any?...) {
        XLog.logImpl(type: "<D>", tag, items: items)
    }
    private static func logImpl(type: String, _ tag: String, items: [Any?]) {
        var logStr = ""
        logStr.append(type)
        logStr.append("[")
        logStr.append(tag)
        logStr.append("]")
        for item in items {
            logStr.append(" ")
            if let i = item as? String {
                logStr.append(i)
            } else if let i = item as? HandyJSON {
                logStr.append(i.toJSONString() ?? "")
            } else {
                logStr.append("\(item ??? "[nil]")")
            }
        }
        #if DEBUG
        print(logStr)
        #endif
    }
}

infix operator ???: NilCoalescingPrecedence

public func ???<T>(optional: T?, defaultValue: @autoclosure () -> String) -> String {
    switch optional {
    case let value?: return String(describing: value)
    case nil: return defaultValue()
    }
}
